var app = new Vue({
    el: '#shopCart',
    data () {
        return {
            /* headerBtn: '编辑', */
            checkedAll: false, // 是否全选
            totalPrice: 0, // 合计价格
            disabled: true, // 下单按钮是否禁用
            checkList: [], // 复选框Data
            isCheck: [], // 复选框是否选中集合
            cartData: null, // 购物车数据
            receiveAddressId: null, // 收货地址Id
            selectStatus: '', // 全选请求参数
            cartBadge: null
        }
    },
    created() {
        this.getAddress()
    },
    watch: {
        isCheck (val) {
            if (val.length > 0) {
                this.disabled = false
            } else {
                this.disabled = true
            }
            let data = this.cartData
            if (val.length === data.length && data.length > 0) {
                this.checkedAll = true
            } else {
                this.checkedAll = false
            }
        }
    },
    methods: {
        getAddress () {
            Axios.post('user/address/getDefaultReceiveAddress.do', {}).then(res => {
                if (res.respCode === 'S') {
                    this.receiveAddressId = res.data.receiveAddressId
                    this.getCartData()
                } else {
                    this.$createDialog({
                        type: 'alert',
                        title: '温馨提示',
                        content: '请填写收货地址！',
                        confirmBtn: {
                            text: '去填写',
                            active: true,
                            disabled: false,
                            href: '../my/my_addshouhuo.html'
                        }
                    }).show()
                }
            })
        },
        getCartData () {
            Axios.post('goods/shopCar/listAdd.do', {}).then(res => {
                if (res.respCode === 'S') {
                    this.checkList = []    
                    this.totalPrice = Number(res.data.totalPrice)
                    let list = res.data.ShoppingCars
                    if (list.length < 100) {
                        this.cartBadge = list.length
                        localStorage.setItem('cartBadge', list.length)
                    } else {
                        this.cartBadge = '99+'
                        localStorage.setItem('cartBadge', '99+')
                    }
                    let cartList = []
                    list.forEach(item => {                        
                        let value = 'cart' + item.shoppingCarId
                        let data = {}
                        if (item.selectStatus === 'SELECTED') {
                            data.checked = value
                            this.isCheck.push(item.shoppingCarId)
                        } else {
                            data.checked = false
                        }
                        data.option = {}
                        data.option.value = value
                        this.checkList.push(data)
                        let goods = {}
                        goods.items = item
                        goods.btns = [
                            {
                                action: 'delete',
                                text: '删除',
                                color: '#d83431'
                            }
                        ]
                        cartList.push(goods)
                    })
                    this.cartData = cartList
                }
            })
        },
        btnCheckedAll () {
            if (this.checkedAll) {
                Axios.post('goods/shopCar/shopcarSelectAll.do', {
                    receiveAddressId: this.receiveAddressId,
                    selectStatus: 'SELECTED'
                }).then(res => {
                    if (res.respCode === 'S') {
                        let list = this.checkList
                        list.map(item => {                            
                            item.checked = item.option.value
                        })
                        let data = this.cartData
                        this.totalPrice = 0
                        this.isCheck = []
                        data.map(item => {
                            item.items.selectStatus = 'SELECTED'
                            this.isCheck.push(item.items.shoppingCarId)
                            this.totalPrice = Number(this.totalPrice) + Number(item.items.minPrice) * item.items.quantity                    
                            this.totalPrice = this.totalPrice.toFixed(2)
                        })
                    } else {
                        this.toast = this.$createToast({
                            txt: res.res.errorMsg,
                            type: 'txt'
                        })
                        this.toast.show()
                        this.checkedAll = false
                    }
                }).catch(() => {
                    this.checkedAll = false
                })
            } else {
                Axios.post('goods/shopCar/shopcarSelectAll.do', {
                    receiveAddressId: this.receiveAddressId,
                    selectStatus: 'NOT_SELECT'
                }).then(res => {
                    if (res.respCode === 'S') {
                        let list = this.checkList
                        list.map(item => {
                            item.checked = false
                        })
                        let data = this.cartData
                        data.map(item => {
                            item.items.selectStatus = 'NOT_SELECT'
                        })
                        this.totalPrice = 0
                        this.isCheck = []
                    } else {
                        this.toast = this.$createToast({
                            txt: res.res.errorMsg,
                            type: 'txt'
                        })
                        this.toast.show()
                        this.checkedAll = true
                    }
                }).catch(() => {
                    this.checkedAll = true
                })
            }            
        },
        onBtnClick(btn, index) {
            if (btn.action === 'delete') {
                this.$createActionSheet({
                    title: '确认要删除吗',
                    active: 0,
                    data: [
                        {content: '删除'}
                    ],
                    onSelect: () => {
                        Axios.post('goods/shopCar/deleteShopcar.do', {
                            shoppingCarId: this.cartData[index].items.shoppingCarId
                        }).then(res => {
                            if (res.respCode === 'S') {
                                this.$refs.swipeItem[index].shrink()
                                setTimeout(() => {
                                    this.checkList.splice(index, 1)
                                    this.isCheck.splice(index, 1)
                                    if (this.cartData[index].items.selectStatus === 'SELECTED') {
                                        let price = Number(this.cartData[index].items.minPrice) * this.cartData[index].items.quantity
                                        this.totalPrice = Number(this.totalPrice) - Number(price.toFixed(2))
                                    }
                                    this.cartData.splice(index, 1)
                                    let cartBadge = this.cartBadge - 1
                                    localStorage.setItem('cartBadge', cartBadge)
                                    this.cartBadge = cartBadge
                                }, 200)
                            }
                        })
                    }
                }).show()
            } else {
                this.$refs.swipeItem[index].shrink()
            }
        },
        reduce (item) {
            let num = item.quantity
            if (item.quantity > 1) {
                num -= 1
                Axios.post('goods/shopCar/updateGoodsNum.do', {
                    quantity: num,
                    shoppingCarId: item.shoppingCarId,
                    selectStatus: item.selectStatus
                }).then(res => {
                    if (res.respCode === 'S') {
                        item.quantity = num
                        if (item.selectStatus === 'SELECTED') {
                            let totalPrice = Number(this.totalPrice) - Number(item.minPrice)
                            this.totalPrice = totalPrice.toFixed(2)
                        }
                    } else {
                        this.toast = this.$createToast({
                            txt: res.res.errorMsg,
                            type: 'txt'
                        })
                        this.toast.show()
                    }
                })
            } else {
                return
            }            
        },
        plus (item) {
            let num = item.quantity
            if (num < 99) {
                num += 1
                Axios.post('goods/shopCar/updateGoodsNum.do', {
                    quantity: num,
                    shoppingCarId: item.shoppingCarId,
                    selectStatus: item.selectStatus
                }).then(res => {
                    if (res.respCode === 'S') {
                        item.quantity = num
                        if (item.selectStatus === 'SELECTED') {
                            let totalPrice = Number(this.totalPrice) + Number(item.minPrice)
                            this.totalPrice = totalPrice.toFixed(2)
                        }
                    } else {
                        this.toast = this.$createToast({
                            txt: res.res.errorMsg,
                            type: 'txt'
                        })
                        this.toast.show()
                    }
                })
            } else {
                return
            }    
        },
        checkGoods (item) {
            let selectStatus = ''
            let totalPrice = Number(this.totalPrice)
            if (item.selectStatus === 'SELECTED') {
                selectStatus = 'NOT_SELECT'
                totalPrice = totalPrice - Number(item.minPrice * item.quantity)
                totalPrice = totalPrice.toFixed(2)
            } else {
                selectStatus = 'SELECTED'
                totalPrice = totalPrice + Number(item.minPrice * item.quantity)
                totalPrice = totalPrice.toFixed(2)
            }
            Axios.post('goods/shopCar/updateGoodsNum.do', {
                quantity: item.quantity,
                shoppingCarId: item.shoppingCarId,
                selectStatus: selectStatus
            }).then(res => {
                if (res.respCode === 'S') {
                    this.totalPrice = Number(totalPrice)
                    item.selectStatus = selectStatus
                    if (selectStatus === 'SELECTED') {
                        this.isCheck.push(item.shoppingCarId)
                    } else {
                        let index = this.isCheck.indexOf(item.shoppingCarId)
                        this.isCheck.splice(index, 1)
                    }
                }
            })
        },
        toConfirm () {
            window.location.href = 'order.html'
        }/* ,
        edit () {           
            let cartList = document.querySelector('.shopCart-orderList')            
            if (this.headerBtn === '编辑') {
                this.headerBtn = '取消'
            } else {
                this.headerBtn = '编辑'
            }
        } */
    }
})